import mount from 'koa-mount';
import serve from 'koa-static';
import sendfile from 'koa-sendfile';
import path from 'path';

import {dev} from './config';


var view_path=path.resolve(__dirname, '../static/');

var router = require('koa-router')();



router.use("*",async function(ctx,next){

	await next();
})

router.get('/login',async (ctx,next)=> {
	let session = await ctx.sess();
	var {next='/'} = ctx.request.query;
	next = /(undefined|null)/.test(next)?'/underlyingAssets/waitingIn/list':next;
	if(session && session.ticket){
		ctx.redirect(next);
		return;
	}
    await sendfile(ctx, `${view_path}/login.html`);
});

router.get('/logout',async (ctx,next)=> {
	ctx.clearSess&&ctx.clearSess();
    await ctx.redirect('/login');
});

router.redirect('/', '/underlyingAssets/waitingIn/list');
// router.get('/home',sendFileIndex);
// router.get('/authority/:path*',sendFileIndex );
// router.get('/room/:path*',sendFileIndex );
// router.get('/user/:path*',sendFileIndex );
// router.get('/loan/:path*',sendFileIndex );
// router.get('/operation/:path*',sendFileIndex );
// router.get('/protocol/:path*',sendFileIndex );
// router.get('/generalize/:path*',sendFileIndex );
// router.get('/devices/:path*',sendFileIndex);
// router.get('/constract/:path*',sendFileIndex);
// router.get('/ads/:path*',sendFileIndex);
// router.get('/announce/:path*',sendFileIndex);
// router.get('/wx/:path*',sendFileIndex);
// router.get('/logs/:path*',sendFileIndex);


router.get('/underlyingAssets/:path*',sendFileIndex);
router.get('/propertyData/:path*',sendFileIndex);
router.get('/systemManage/:path*',sendFileIndex);

async function sendFileIndex(ctx, next){
	if(!ctx.ticket){
		let next = ctx.url;
		next = /(undefined|null)/.test(next)?'/underlyingAssets/waitingIn/list':next;
		ctx.redirect('/login?next='+next);
		return;
	}
	console.log("-----ddd-----")
	await sendfile(ctx, `${view_path}/index.html`);
}

export default function(app){
    app.use(serve(__dirname+ '/'+ '../dist'));
    app.use(mount('/resource', serve(__dirname + '/' + '../../resource')));
    app.use(mount('/node_modules', serve(__dirname + '/' + '../node_modules')));
    app.use(router.routes()).use(router.allowedMethods());
}
